using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using T2.WebSecurity.ASPMVC.Entity;
using T2.WebSecurity.ASPMVC.Entity.Interface;
using T2.WebSecurity.ASPMVC.Entity.Search.Interface;
using T2.WebSecurity.ASPMVC.Entity.Translators;
using T2.WebSecurity.ASPMVC.Entity.Translators.Search;
using T2.WebSecurity.Business.Entities;
using T2.WebSecurity.Business.Entities.Interfaces;
using T2.WebSecurity.Business.Entities.Search.Interfaces;
using T2.WebSecurity.Business.Workflows;


namespace T2.WebSecurity.ASPMVC.ViewModel
{

    public class ApplicationRolesViewModel : BaseViewModel
	{
		Business.BusinessService bs = new Business.BusinessService();
		public ApplicationRolesViewModel(bool loadParent)
		{
		if (loadParent)
			{
				this.LoadApplicationList();
			}
		}
		
		private String m_header;
		public String Header
        {
            get
            {
                return "Application Roles";
            }
			
			set
            {
                m_header = value;
            }
        }
		
		private String m_title;
		public String Title
        {
            get
            {
                return "Application Roles";
            }
			
			set
            {
                m_title = value;
            }
        }
		
		private IApplicationRoles_ui m_model;
		public IApplicationRoles_ui Model
		{
			get
			{
				return m_model;
			}
			set
			{
				m_model = value;
			}
		}
		private List<IApplication_ui> m_applicationlist;
		public List<IApplication_ui> Application_List
		{
			get
			{
				return m_applicationlist;
			}
			set
			{
				m_applicationlist = value;
			}
		}

		 private void LoadApplicationList()
		{
            IApplication_biz bizParam = new Application_biz();
            bizParam.Action = "GetAll";
            bizParam.PageNumber = 1;
            bizParam.PageSize = 20;
            bizParam.SortColumn = "ApplicationID";
            bizParam.SortType = "desc";
            bizParam.TotalRows = -1;

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("ParamSearch", null);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserName", User.UserName);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new ApplicationQuery(), wfInput);

            IApplication_biz[] retValBiz = (IApplication_biz[])wfOutput["RetValCollection"];

			//List<IApplication_biz> retVal = bs.ApplicationController_Get(param, true, true);
			List<IApplication_ui> LoadRetVal = new List<IApplication_ui>();
			ApplicationUITranslator trans = new ApplicationUITranslator();
            foreach (IApplication_biz item in retValBiz)
			{
				IApplication_ui temp = new Application_ui();
				temp = trans.TranslateFromBusinessLayer(item);
				LoadRetVal.Add(temp);
			}
			m_applicationlist = LoadRetVal;
		}


        
		
		public void LoadModel(IApplicationRoles_ui param)
        {
            ApplicationRolesUITranslator trans = new ApplicationRolesUITranslator();
            IApplicationRoles_biz bizParam = trans.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("ParamSearch", null);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserId", (int)User.ProviderUserKey);
            wfInput.Add("UserName", User.UserName);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new ApplicationRolesQuery(), wfInput);

            IApplicationRoles_biz retValBiz = (IApplicationRoles_biz)wfOutput["RetVal"];

            //List<IApplicationRoles_biz> tempRetVal = bs.ApplicationRolesController_Get(bizParam, true, true);
            IApplicationRoles_ui retVal = new ApplicationRoles_ui();
            if (retValBiz != null)
            {
                retVal = trans.TranslateFromBusinessLayer(retValBiz);
            }

            m_model = retVal;

        }

        public Int32 Set(IApplicationRoles_ui param)
        {
            ApplicationRolesUITranslator trans = new ApplicationRolesUITranslator();
            IApplicationRoles_biz bizParam = trans.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserId", (int)User.ProviderUserKey);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new ApplicationRolesNonQuery(), wfInput);

            IDictionary<string, int> r = (IDictionary<string,int>)wfOutput["RetVal"];


            //IDictionary<string,int> r = bs.ApplicationRolesController_Set(bizParam);
            var ret = r.ToList().Select(s => new { Key = s.Key, Value = s.Value });
            Int32 retVal = ret.Where(z => z.Key == "Return_Value").Select(m => m.Value).FirstOrDefault();
			return retVal;
        }

       

        public List<IApplicationRoles_ui> SearchList(IApplicationRolesSearch_ui param)
        {
            ApplicationRolesUISearchTranslator transSearch = new ApplicationRolesUISearchTranslator();
            IApplicationRolesSearch_biz bizParam = transSearch.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            //if (HttpContext.Current.Session["ApplicationId"] == null)
            //{
            //    throw new Exception("Application is not selected");
            //}
            //int ApplicationID = Convert.ToInt32(HttpContext.Current.Session["ApplicationId"]);

            //wfInput.Add("AppId", ApplicationID);
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", null);
            wfInput.Add("ParamSearch", bizParam);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserId", (int)User.ProviderUserKey);
            wfInput.Add("UserName", User.UserName);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new ApplicationRolesQuery(), wfInput);

            IApplicationRoles_biz[] retValBiz = (IApplicationRoles_biz[])wfOutput["RetValCollection"];

            //List<IApplicationRoles_biz> retValBiz = bs.ApplicationRolesController_Search(bizParam, true, true);
            List<IApplicationRoles_ui> retVal = new List<IApplicationRoles_ui>();
            ApplicationRolesUITranslator trans = new ApplicationRolesUITranslator();
            foreach (IApplicationRoles_biz item in retValBiz)
            {
                IApplicationRoles_ui temp = trans.TranslateFromBusinessLayer(item);
                retVal.Add(temp);
            }

            
            return retVal;

        }

        

        public List<IApplicationRoles_ui> ModelList(IApplicationRoles_ui param)
        {

            ApplicationRolesUITranslator trans = new ApplicationRolesUITranslator();
            IApplicationRoles_biz bizParam = trans.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("ParamSearch", null);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserId", (int)User.ProviderUserKey);
            wfInput.Add("UserName", User.UserName);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new ApplicationRolesQuery(), wfInput);

            IApplicationRoles_biz[] retValBiz = (IApplicationRoles_biz[])wfOutput["RetValCollection"];

            //List<IApplicationRoles_biz> retValBiz = bs.ApplicationRolesController_Get(bizParam, true, true);

            List<IApplicationRoles_ui> retVal = new List<IApplicationRoles_ui>();
            foreach (IApplicationRoles_biz item in retValBiz)
            {
                IApplicationRoles_ui temp = trans.TranslateFromBusinessLayer(item);
                retVal.Add(temp);
            }

            
            return retVal;

        }
		
	}
}
